//
//  PotentialDetailViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/10/11.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PagedViewController: BaseViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate,ChangePage {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        if let idx = ctls.index(of: viewController) {
            
            if idx > 0 {
                
                return ctls[idx - 1]
                
            } else {
                
                return nil
                
            }
        }
        
        return nil
        
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        if let idx = ctls.index(of: viewController) {
            
            if idx < ctls.count - 1 {
                
                return ctls[idx + 1]
                
            } else {
                
                return nil
                
            }
        }
        
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            
            let vc = pageViewController.viewControllers![0]
            
            if let index = ctls.index(of: vc) {
                
                trayView.slide(toPosition: index)
                
            }
            
        }
    }

    var ctls: Array<UIViewController> = []
    
    public let page = UIPageViewController.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    
    let trayView = SlidePageView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: UIScreen.main.bounds.size.width, height: 55)))
    
    func change(toPage idx: Int) {
        
        if let vc = page.viewControllers?[0] {
            
            if let currentIndex = ctls.index(of: vc) {
                
                if currentIndex < idx {
                    
                    page.setViewControllers([ctls[idx]], direction: .forward, animated: true, completion: nil)
                    
                } else {
                    page.setViewControllers([ctls[idx]], direction: .reverse, animated: true, completion: nil)
                }
                
            }
        }
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        trayView.delegate = self
        
        trayView.backgroundColor = UIColor.white
        
        self.view.addSubview(trayView)
        
        trayView.configTitles(configTitles())
        
        ctls = configViewControllers()
        
        page.delegate = self
        
        page.dataSource = self
        
        for view in page.view.subviews {
            if let v = view as? UIScrollView {
                v.delegate = trayView
            }
        }
        
        
        
        page.setViewControllers([ctls[0]], direction: UIPageViewController.NavigationDirection.forward, animated: true, completion: nil)
        
        self.view.addSubview(page.view)
        
        for view in self.view.subviews {
            if view is UIScrollView {
                let scroll = view as! UIScrollView
                scroll.bounces = false
            }
        }
        
        // Do any additional setup after loading the view.
    }
    
    func configTitles() -> Array<String> {
        return []
    }
    
    func configViewControllers() -> [UIViewController] {
        return []
    }
    
    override func viewWillLayoutSubviews() {
        
        super.viewWillLayoutSubviews()
        
        page.view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: trayView.bottom), size: CGSize.init(width: self.view.width, height: self.view.height - 55))
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
